;NSIS Jabr Installer
;Created by XyC

!include "MUI.nsh"

Name "Jabr"
;OutFile "Jabr_install.exe"
InstallDir "$PROGRAMFILES\Jabr"
InstallDirRegKey HKLM "Software\Jabr" "InstallDir"

Var MUI_TEMP
Var STARTMENU_FOLDER

!define BASE_DIR "..\bin\install_tmp"
!define CORE_DIR "Core"
!define RESOURCE_DIR "Resources"
!define HELP_DIR "Help"

!define MUI_ABORTWARNING
!define MUI_COMPONENTSPAGE_SMALLDESC
!define MUI_STARTMENUPAGE_NODISABLE
!define MUI_STARTMENUPAGE_REGISTRY_ROOT "HKLM" 
!define MUI_STARTMENUPAGE_REGISTRY_KEY "Software\Jabr" 
!define MUI_STARTMENUPAGE_REGISTRY_VALUENAME "StartMenuDir"

!insertmacro MUI_PAGE_LICENSE "license.txt"
!insertmacro MUI_PAGE_COMPONENTS
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_STARTMENU Application $STARTMENU_FOLDER
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES
  
!insertmacro MUI_LANGUAGE "English"

InstType "Full"
InstType "Minimal"
InstType "Typical"

SectionGroup "Main files" MainFiles
  Section "Application core files" CoreFiles
    SectionIn 1 2 3 RO
    SetOutPath "$INSTDIR"
    File "${BASE_DIR}\${CORE_DIR}\*"
    WriteRegStr HKLM SOFTWARE\Jabr "InstallDir" "$INSTDIR"
    WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Jabr" "DisplayName" "Jabr"
    WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Jabr" "UninstallString" '"$INSTDIR\uninstall.exe"'
    WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Jabr" "NoModify" 1
    WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Jabr" "NoRepair" 1
    WriteUninstaller "$INSTDIR\Uninstall.exe"
    !insertmacro MUI_STARTMENU_WRITE_BEGIN Application
      CreateDirectory "$SMPROGRAMS\$STARTMENU_FOLDER"
      CreateShortCut "$SMPROGRAMS\$STARTMENU_FOLDER\Uninstall.lnk" "$INSTDIR\Uninstall.exe"
      CreateShortCut "$SMPROGRAMS\$STARTMENU_FOLDER\Jabr.lnk" "$INSTDIR\Jabr.exe"
    !insertmacro MUI_STARTMENU_WRITE_END
  SectionEnd
  Section "Required resources" ResourceFiles
    SectionIn 1 2 3 RO
    SetOutPath "$INSTDIR\${RESOURCE_DIR}"
    File /r "${BASE_DIR}\${RESOURCE_DIR}\*"
  SectionEnd
  Section "Help and documentation" HelpFiles
    SectionIn 1 3
    SetOutPath "$INSTDIR\${HELP_DIR}"
    File /r "${BASE_DIR}\${HELP_DIR}\*"
  SectionEnd
SectionGroupEnd

LangString DESC_MainFiles ${LANG_ENGLISH} "Main files required to run the application."
LangString DESC_CoreFiles ${LANG_ENGLISH} "Application executable and all required libraries."
LangString DESC_ResourceFiles ${LANG_ENGLISH} "Resources required by the application. Images, data and configuration files."
LangString DESC_HelpFiles ${LANG_ENGLISH} "Help files and documentation for the end user."
;LangString DESC_Plugins ${LANG_ENGLISH} "Extra features and functionality."
;LangString DESC_GG ${LANG_ENGLISH} "Gadu-Gadu protocol support."

!insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN
  !insertmacro MUI_DESCRIPTION_TEXT ${MainFiles} $(DESC_MainFiles)
  !insertmacro MUI_DESCRIPTION_TEXT ${CoreFiles} $(DESC_CoreFiles)
  !insertmacro MUI_DESCRIPTION_TEXT ${ResourceFiles} $(DESC_ResourceFiles)
  !insertmacro MUI_DESCRIPTION_TEXT ${HelpFiles} $(DESC_HelpFiles)
!insertmacro MUI_FUNCTION_DESCRIPTION_END

Section "Uninstall"
  RMDir /r /REBOOTOK "$INSTDIR\${RESOURCE_DIR}"
  RMDir /r /REBOOTOK "$INSTDIR\${HELP_DIR}"
  Delete /REBOOTOK "$INSTDIR\*.*"
  RMDir /REBOOTOK "$INSTDIR"
  !insertmacro MUI_STARTMENU_GETFOLDER Application $MUI_TEMP
  Delete "$SMPROGRAMS\$MUI_TEMP\*.*"
  StrCpy $MUI_TEMP "$SMPROGRAMS\$MUI_TEMP"
  startMenuDeleteLoop:
	ClearErrors
    RMDir $MUI_TEMP
    GetFullPathName $MUI_TEMP "$MUI_TEMP\.."    
    IfErrors startMenuDeleteLoopDone
    StrCmp $MUI_TEMP $SMPROGRAMS startMenuDeleteLoopDone startMenuDeleteLoop
  startMenuDeleteLoopDone:
  DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Jabr"
  DeleteRegKey HKLM "Software\Jabr"
SectionEnd